package cn.edu.lingnan.servlet.project;

import cn.edu.lingnan.pojo.Project;
import cn.edu.lingnan.pojo.User;
import cn.edu.lingnan.service.ProjectService;
import cn.edu.lingnan.service.ProjectServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet({"/addprojectservlet","/stdentaddprojectservlet"})
public class AddProjectServlet extends HttpServlet {
    private ProjectService projectService= new ProjectServiceImp();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getServletPath();
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        switch (action) {
            case "/addprojectservlet":
                maneger_addproject(request, response);
                break;
            case "/stdentaddprojectservlet":
                studnet_addproject(request, response);
                break;
        }
    }

    public void maneger_addproject(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        // 获取表单数据
        Integer protectId = Integer.parseInt(request.getParameter("protectId"));
        String projectName = request.getParameter("projectName");
        Integer postId = Integer.parseInt(request.getParameter("postId"));
        String postName = request.getParameter("postName");
        String type = request.getParameter("type");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String description = request.getParameter("description");
        String members = request.getParameter("members");

        Project project = new Project();
        project.setProtectId(protectId);
        project.setProjectName(projectName);
        project.setPostId(postId);
        project.setPostName(postName);
        project.setType(type);
        project.setStartTime(startTime);
        project.setEndTime(endTime);
        project.setDescription(description);
        project.setMembers(members);

        Integer num = projectService.AdminInsertProject(project);

        if (num != null && num > 0) {
            // 项目添加成功
            request.setAttribute("message", "项目添加成功");
        } else {
            // 项目添加失败
            request.setAttribute("message", "项目添加失败");
        }

        // 转发回原来的 JSP 页面
        request.getRequestDispatcher("/manager_addproject.jsp").forward(request, response);
    }

 public void studnet_addproject(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     request.setCharacterEncoding("UTF-8");
     response.setCharacterEncoding("UTF-8");
     // 获取表单数据
     Integer protectId = Integer.parseInt(request.getParameter("protectId"));
     String projectName = request.getParameter("projectName");
     Integer postId = Integer.parseInt(request.getParameter("postId"));
     String postName = request.getParameter("postName");
     String type = request.getParameter("type");
     String startTime = request.getParameter("startTime");
     String endTime = request.getParameter("endTime");
     String description = request.getParameter("description");
     String leader = request.getParameter("leader");
     String member = request.getParameter("members");
     String members ="队长:"+leader + ","+"成员:" + member;

     Project project = new Project();
     project.setProtectId(protectId);
     project.setProjectName(projectName);
     project.setPostId(postId);
     project.setPostName(postName);
     project.setType(type);
     project.setStartTime(startTime);
     project.setEndTime(endTime);
     project.setDescription(description);
     project.setMembers(members);

     Integer num = projectService.StudnetCreateProject(project);
     HttpSession session = request.getSession();
     User user= (User) session.getAttribute("user");
     List<Project> AdultProjects =      projectService.StudentQueryCreateAdultProject(user.getName());
     request.getSession().setAttribute("AdultProjects", AdultProjects);

     if (num != null && num > 0) {
         // 项目添加成功
         request.setAttribute("message", "项目添加成功");
     } else {
         // 项目添加失败
         request.setAttribute("message", "项目添加失败");
     }

     // 转发回原来的 JSP 页面
     request.getRequestDispatcher("/student_createProject.jsp").forward(request, response);
 }
}
